package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.SysLikeRecordResp;
import com.ruoyi.system.enums.LikeRecordEnum;
import com.ruoyi.system.service.ISysArticleService;
import com.ruoyi.system.service.ISysCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysLikeRecordMapper;
import com.ruoyi.system.domain.SysLikeRecord;
import com.ruoyi.system.service.ISysLikeRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 点赞记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-02
 */
@Service
public class SysLikeRecordServiceImpl implements ISysLikeRecordService 
{
    @Autowired
    private SysLikeRecordMapper sysLikeRecordMapper;
    @Autowired
    private ISysArticleService articleService;
    @Autowired
    private ISysCommentService commentService;

    /**
     * 查询点赞记录
     * 
     * @param userId 点赞记录主键
     * @return 点赞记录
     */
    @Override
    public SysLikeRecord selectSysLikeRecordByUserId(Long userId)
    {
        return sysLikeRecordMapper.selectSysLikeRecordByUserId(userId);
    }

    /**
     * 查询点赞记录列表
     * 
     * @param sysLikeRecord 点赞记录
     * @return 点赞记录
     */
    @Override
    public List<SysLikeRecordResp> selectSysLikeRecordList(SysLikeRecord sysLikeRecord)
    {
        return sysLikeRecordMapper.selectSysLikeRecordList(sysLikeRecord).stream().map(item -> {
            SysLikeRecordResp resp = BeanUtil.copyProperties(item, SysLikeRecordResp.class);
            resp.setTypeName(LikeRecordEnum.getByKey(item.getType()));
            return resp;
        }).collect(Collectors.toList());
    }

    /**
     * 新增点赞记录
     * 
     * @param sysLikeRecord 点赞记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysLikeRecord(SysLikeRecord sysLikeRecord)
    {
        Long userId = SecurityUtils.getUserId();
        SysLikeRecord find = sysLikeRecordMapper.selectSysLikeRecordByUserIdAndTargetId(userId, sysLikeRecord.getTargetId(), sysLikeRecord.getType());
        if (Objects.nonNull(find)) {
            sysLikeRecordMapper.deleteSysLikeRecordByUserIdAndTargetId(userId, sysLikeRecord.getTargetId(), sysLikeRecord.getType());
            return 0;
        }
        sysLikeRecord.setCreateTime(DateUtils.getNowDate());
        sysLikeRecord.setUserId(SecurityUtils.getUserId());
        return sysLikeRecordMapper.insertSysLikeRecord(sysLikeRecord);
    }

    /**
     * 修改点赞记录
     * 
     * @param sysLikeRecord 点赞记录
     * @return 结果
     */
    @Override
    public int updateSysLikeRecord(SysLikeRecord sysLikeRecord)
    {
        return sysLikeRecordMapper.updateSysLikeRecord(sysLikeRecord);
    }

    /**
     * 批量删除点赞记录
     * 
     * @param userIds 需要删除的点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteSysLikeRecordByUserIds(Long[] userIds)
    {
        return sysLikeRecordMapper.deleteSysLikeRecordByUserIds(userIds);
    }

    /**
     * 删除点赞记录信息
     * 
     * @param userId 点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteSysLikeRecordByUserId(Long userId)
    {
        return sysLikeRecordMapper.deleteSysLikeRecordByUserId(userId);
    }

    @Override
    public Integer selectSysLikeRecordByType(Integer type) {
        return sysLikeRecordMapper.selectSysLikeRecordByType(type);
    }

    @Override
    public Integer selectSysLikeRecordByTargetIdAndType(Long targetId, Integer type) {
        return sysLikeRecordMapper.selectSysLikeRecordByTargetIdAndType(targetId, type);
    }

    @Override
    public SysLikeRecord selectSysLikeRecordByUserIdAndTargetIdAndType(Long userId, Long targetId, Integer type) {
        return sysLikeRecordMapper.selectSysLikeRecordByUserIdAndTargetId(userId, targetId, type);
    }
}
